<?php

/**
 * 商城订单
 */
namespace frontend\controllers\sale;

use bc\controller\SampleController;
use bc\controller\WxOAuthController;
use bc\model\OrderAddressModel;
use bc\model\OrderModel;
use bc\model\wc\SpreadModel;
use bc\model\wc\StaffModel;
use yii\web\Session;
use bc\model\wc\BookingModel;
use bc\model\wc\DoctorModel;
use bc\model\wc\BpointModel;
use bc\model\ecs\UserModel as ecsUser;
use bc\wx\User as User;
use bc\model\UserModel as zUser;
use bc\model\wx\report\CodeModel;
use bc\util\Sms;
use bc\model\ProductModel;
use bc\model\order\DetailModel;
use bc\model\AddressModel;
use bc\model\BindModel;
use bc\model\product\DistributionModel;
use bc\service\SpokesmanService;
use bc\util\Math;
use bc\service\CouponService;
use bc\model\coupons\CouponsModel;
use bc\model\coupons\CouponsUserModel;
use bc\entity\Order;
use bc\entity\Coupon;
use bc\model\coupons\OrderLogModel;

class OrderController extends SampleController
{
	protected $payUrl = '/sale/order/savedate';
	
	protected $bindIds = [2 , 3];
	
	public function actionIndex()
	{
		$orderModel = new OrderModel ();
		$orderDetailModel = new DetailModel ();
		$productModel = new ProductModel ();
		
		$session = new Session ();
		$cart = json_decode ( $session->get ( 'cart' ), true );
		
		$ids = [ ];
		if ($cart)
		{
			foreach ( $cart as $id )
			{
				array_push ( $ids, $id );
			}
		}
		
		$ids = [ 1, 2];
		$products = $productModel->getProductCollection ( $ids );
		// echo '<pre>';
		// print_r($products);
		
		$view ['products'] = $products;
		return $this->render ( 'index' );
	}
	
	/**
	 * 快速购买 将商品id和数量写入session内
	 */
	public function actionQuickAdd()
	{
		$session = new Session();
		$product_id = $this->getParam ( 'product_id', 0 );
		$num = $this->getParam ( 'num', 1 );

		if ($num < 1)
			$num = 1;
		
		if($product_id)
		{
			//将商品和数量写入session
			$cart['product_id'] = $product_id;
			$cart['num'] = $num;
			$session->set('quick_cart', json_encode($cart));
		}
		$this->redirect('/sale/order/quick');
	}
	
	/**
	 * 快速购买
	 */
	public function actionQuick()
	{
		$session = new Session();
		$shop_user = $session->get('shop_user');
		$cart = json_decode($session->get('quick_cart'), true);
		$address_id = $this->get('address_id', 0);
		
		if(!$cart)
			$this->redirect('/');
		
		$sessionAddress = $session->get('address_id');
		
		// 商品信息
		$productModel = new ProductModel ();
		$product = $productModel->getRowById ( $cart['product_id'] );

		$num = $cart['num'];
		
		if (! $product || $product ['is_sale'] == 2 || $product ['status'] == 2)
			$this->redirect ( '/' );
			
		$user_id = $shop_user['user_id'];
		
		$view ['product'] = $product;
		
		$product_price = $product['product_price'];
		$this->getHtmlFromPid($product['product_id']);
		//代言人商品
		$distributionModel = new DistributionModel();
		$distribution = $distributionModel->getRow(['product_id' => $product['product_id']]);
		if($distribution)
		{
			$view['distribution'] = $distribution;
			$product_price = $distribution['distribution_price'];
			
		}
		
		
		//推荐人购买商品信息
		$spokesmanService = new SpokesmanService();
		
		$recommendProductArr = $session->get('recommend_product');
		$recommendProduct = $recommendProductArr[$cart['product_id']];
		if($recommendProduct && $spokesmanService->firstBuy($product['product_id'], $shop_user))
		{
			$product_price = $distribution['distribution_price'] - Math::ConverToCeil($distribution['distribution_price'] * $distribution['first_discount'], 1);
			
		}
		
		// 用户默认收货地址
		$addressModel = new AddressModel ();
		if($address_id)
		{
			//选择自定义收货地址
			$defaultAddress = $addressModel->getRow(['id' => $address_id, 'uid' => $user_id, 'is_invalid' => 1]);
		}
		else 
		{
			$defaultAddress = $addressModel->getRow ( [
					'uid' => $user_id,
					'default_address' => 1,
					'is_invalid' => 1
			] );
		}
		
		$pay_price = $product_price * $num ;
		
		
		//获取用户手机
		$ecsUserModel = new ecsUser();
		$ecs_user = $ecsUserModel->getRowById($shop_user['user_id']);
		
		
		//获取优惠券
		if(strpos($this->getRequest()->getReferrer(), '/sale/order/choosecoupon'))
		{
			$couponSession = $session->get('coupon');
			
			
			$couponModel = new CouponsModel();
			$coupon = $couponModel->getRow(['coupons_id' => $couponSession['coupons_id'], 'is_del' => 0]);
			$currentDate = date('Y-m-d H:i:s');
			if($coupon['use_start_time'] <= $currentDate && $coupon['use_end_time'] >= $currentDate)
			{
				$objOrder = new Order();
				$objCoupon = new Coupon();
				
				$objOrder->user_id = $user_id;
				$objOrder->group_id = 0;
				$objOrder->price = $pay_price;
				$objOrder->product_id = $product['product_id'];
				
				
				$objCoupon->coupon_id = $coupon['coupons_user_id'];
				$objCoupon->coupons_money = $coupon['coupons_money'];
				$objCoupon->coupons_use_target = $coupon['coupons_use_target'];
				$objCoupon->coupons_type = $coupon['coupons_type'];
				$objCoupon->product_group_id = 0;
				$objCoupon->product_id = $coupon['product_id'];
				
				$couponService = new CouponService();
				$couponService->setCoupon($objCoupon);
				$couponService->setOrder($objOrder);
				$pay_price = $couponService->checkout();
				
				
				if($coupon['coupons_money'] > $objOrder->price )
				{
					$coupon['coupons_money'] = $objOrder->price;
				}
				
				
				$view['coupon'] = $coupon;
			}
		}
		
		
		//统计几张可以使用的优惠券
		$couponService = new CouponService();
		$couponParams['user_id'] = $user_id;
		$couponParams['product_id'] = $product['product_id'];
		$couponParams['use_start_time'] = $couponParams['use_end_time'] = date('Y-m-d H:i:s');
		$couponParams['product_price'] = $pay_price;
		$unUseList = $couponService->getAvailableList($couponParams);
		
		
		$unUseTotal = 0;
		foreach ($unUseList as $unUse)
		{
			$unUseTotal += $unUse['total'];
		}
		
		
		$view ['product_price'] = $product_price;
		$view ['num'] = $num;
		$view ['default_address'] = $defaultAddress;
		$view ['mobile'] = $ecs_user['mobile'] ? $ecs_user['mobile'] : '';
		$view ['product_html']=$this->html;
		$view ['pay_price'] = $pay_price;
		$view ['unUseTotal'] = $unUseTotal;
		return $this->render ( 'quick', $view );
	}
	
	/**
	 * 快速购买 - 生成订单
	 * 
	 */
	public function actionQuickOrder()
	{
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		$product_id = $this->post('product_id');
		$num = $this->post('num');
		$address_id = $this->post('address_id');
		$coupon_id = $this->post('coupon_id', 0);
		
		//将地址映射到bc_order_address表
		$addressM = new AddressModel();
		$addressD=$addressM->getRowById($address_id);
		unset($addressD['id']);
		$orderaddressM = new OrderAddressModel();
		$order_address_id=$orderaddressM ->addAdd($addressD);
		$orderNo = date ( 'YmdHis' ) . rand ( 10000000, 99999999 );
		
		$productModel = new ProductModel();
		$orderModel = new OrderModel();
		$orderDetailModel = new DetailModel();
		
		$product = $productModel->getRowById($product_id);
		
		$payAmount = $product['product_price'] * $num;
		$orderAmount = $product['product_price'] * $num;
		$productPrice = $product['product_price'];
		
		if (! $product || $product ['is_sale'] == 2 || $product ['status'] == 2)
			$this->redirect ( '/' );

		//判断当前用户是否有这个产品的下单，并没有支付，如果有下单没有支付，就更新
		//如果没有就插入
		
			
		//通过分享二维码首次购买商品
		$distributionModel = new DistributionModel();
		$distribution = $distributionModel->getRow(['product_id' => $product['product_id']]);
		if($distribution)
		{
			
			$spokesmanService = new SpokesmanService();
			
			$recommendProductArr = $session->get('recommend_product');
			$recommendProduct = $recommendProductArr[$product_id];
			if($recommendProduct && $spokesmanService->firstBuy($product_id, $user))
			{
					
				$productPrice = $distribution['distribution_price'] - Math::ConverToCeil($distribution['distribution_price'] * $distribution['first_discount'], 1);
				$payAmount =  $productPrice * $num;
			}
			else 
			{
				$payAmount =  $distribution['distribution_price'] * $num;
			}
		}
		
		//销毁session
		$session->set('coupon', null);
		
			
		//$orderinfo=$orderModel->checkBuyProduct($user['user_id'],$product_id);
		//if(empty($orderinfo))
		//用户有订单列表，不需要再更新订单
		if(true)
		{
			
			
			
			//使用了优惠券
			if($coupon_id)
			{
				$couponService = new CouponService();
				$couponUserModel = new CouponsUserModel();
				$couponUserModel->columns = $couponService->columns();
				$couponParams = CouponsModel::getTableSchema()->name . '.coupons_id = ' . $coupon_id;
				$couponParams .=  ' AND ' . CouponsUserModel::getTableSchema()->name . '.status = 0';
				$couponParams .=  ' AND ' . CouponsUserModel::getTableSchema()->name . '.is_del = 0';
				$couponParams .=  ' AND ' . CouponsUserModel::getTableSchema()->name. '.user_id = ' . $user['user_id'];
				$couponRow = $couponUserModel->getUseCoupon($couponParams);
				
				if($couponRow)
				{
					$order = new Order();
					$coupon = new Coupon();
						
					$order->user_id = $user['user_id'];
					$order->group_id = 0;
					$order->price = $payAmount;
					$order->product_id = $product['product_id'];
						
						
					$coupon->coupon_id = $couponRow['cuserid'];
					$coupon->coupons_money = $couponRow['coupons_money'];
					$coupon->coupons_use_target = $couponRow['coupons_use_target'];
					$coupon->coupons_type = $couponRow['coupons_type'];
					$coupon->product_group_id = $couponRow['product_group_id'];
					$coupon->product_id = $couponRow['product_id'];
						
					$couponService->setCoupon($coupon);
					$couponService->setOrder($order);
					$payAmount = $couponService->checkout();
						
					
					//更新优惠券
					$couponData['status'] = 1;
					$couponUserModel->modify($couponData, $couponRow['cuserid']);
					
				}
				
			}
			
			
			$orderData['user_id'] = $user['user_id'];
			$orderData['address_id'] = $order_address_id;
			$orderData['order_no'] = $orderNo;
			$orderData['order_status'] = 1;
			$orderData['shipping_status'] = 1;
			$orderData['pay_amount'] = $payAmount;
			$orderData['order_amount'] = $orderAmount;
			$orderData['pay_status'] = 0;
			$orderData['pay_time'] = 0;
			$orderData['create_time'] = time();
			$orderData['update_time'] = time();
			$order_id = $orderModel->add($orderData);
			
			
			$orderDetailData['order_id'] = $order_id;
			$orderDetailData['product_id'] = $product_id;
			$orderDetailData['product_num'] = $num;
			$orderDetailData['product_price'] = $product['product_price'];
			$orderDetailData['create_time'] = time();
			$orderDetailModel->add($orderDetailData);
			
			
			//更新订单主键到订单优惠券操作表
			if($couponRow)
			{
				$orderLogModel = new OrderLogModel();
				$orderLogModel->add(['order_id' => $order_id, 'coupon_user_id' => $couponRow['cuserid'], 'user_id' => $user['user_id'] , 'is_del' => 0, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s')]);
			}
			
			
			//绑定样本
			if(in_array($product_id, $this->bindIds))
			{
				for ($i=0; $i < $num; $i++)
				{
					$bindModel = new BindModel();
//					$bindData['order_id'] = $order_id;
//					$bindData['create_time'] = time();
//					$bindData['product_id'] = $product_id;
//					$bindModel->add($bindData);
					$sql = 'insert into bc_order_bind (order_id,product_id,create_time) values('.$order_id.','.$product_id.','.time().');';
					$bindModel->getDb()->createCommand($sql)->execute();
				}

			}
		}
		
		
		$this->redirect('/wxpay/?id='.$order_id);
	}
	
	/**
	 */
	public function actionBooking()
	{
		$session = new Session ();
		$user = $session->get ( 'user' );
		
		$id = $this->get ( 'id' );
		
		// 设定默认的订单号
		$orderNo = date ( 'YmdHis' ) . rand ( 10000000, 99999999 );
		if ($id)
		{
			
			// 页面数据信息
			$bookingModel = new BookingModel ();
			$whereUserModel = array (
					'id' => $id 
			);
			$bookingModelRow = $bookingModel->getRow ( $whereUserModel );
			
			$userModel = new zUser ();
			$whereUserModel = array (
					'id' => $bookingModelRow ['user_id'] 
			);
			$userModelRow = $userModel->getRow ( $whereUserModel );
			$bookingModelRow ['name'] = $userModelRow ['real_name'];
			$bookingModelRow ['idcard'] = $userModelRow ['idcard'];
			$bookingModelRow ['idcard_type'] = $userModelRow ['idcard_type'];
			$bookingModelRow ['birthday'] = $userModelRow ['birthday'];
			$bookingModelRow ['mobile'] = $userModelRow ['mobile'];
			
			$orderNo = $bookingModelRow ['order_no'];
			$view ['row'] = $bookingModelRow;
			// end
		}
		
		// 获取采血点和医生
		$bpointModel = new BpointModel ();
		$bpointList = $bpointModel->getList ( array (
				'is_invalid' => 1 
		) );
		
		$doctorModel = new DoctorModel ();
		$doctorModel->columns = 'bc_wc_doctor.id as d_id,user_id,hospital_id,phone,bc_wc_hospital.id as h_id,name,address,bc_user.id as uid,real_name,nick_name,wxid';
		$doctorList = $doctorModel->getDoctorList ( array (
				'bc_wc_doctor.is_invalid' => 1 
		) );
		
		$view ['bpointList'] = $bpointList;
		$view ['doctorList'] = $doctorList;
		$view ['payUrl'] = $this->payUrl;
		return $this->render ( 'booking', $view );
	}
	
	/**
	 * 页面数据保存
	 */
	public function actionSavedate()
	{
		$session = new Session ();
		$user = $session->get ( 'user' );
		$shop_user = $session->get ( 'shop_user' );
		$userDate ['real_name'] = $this->post ( 'name', '' );
		$userDate ['birthday'] = $this->post ( 'birthday', '' );
		$userDate ['mobile'] = $this->post ( 'mobile', '' );
		$userDate ['idcard'] = $this->post ( 'idcard', '' );
		$userDate ['idcard_type'] = $this->post ( 'idcard_type', '' );
		$userDate ['update_time'] = date ( 'Y-m-d H:i:s', time () );
		$userDate ['is_invalid'] = 1;
		// 更新bc_user表
		$userModel = new zUser ();
		$whereUserModel = array (
				'wxid' => $user ['openid'] 
		);
		$userModelRow = $userModel->getRow ( $whereUserModel );
		$userModel->modify ( $userDate, $userModelRow ['id'] );
		
		$data ['childbirth_date'] = $this->post ( 'duedate' );
		$data ['bpoint_id'] = $this->post ( 'collection' );
		// $data['doctor_id'] = $this->post('doctor');
		$data ['user_id'] = $userModelRow ['id'];
		$data ['update_time'] = time ();
		$data ['is_invalid'] = 1;
		$data ['status'] = 1;
		$data ['report_url'] = "";
		
		$bookingModel = new BookingModel ();
		$whereUserModel = array (
				'user_id' => $userModelRow ['id'] 
		);
		$bookingModelRow = $bookingModel->getRow ( $whereUserModel );
		if (empty ( $bookingModelRow ))
		{
			
			// 查看用户是那个医生推广的。
			$whereSpreadModel = array (
					'user_penid' => $user ['openid'] 
			);
			$spreadM = new SpreadModel ();
			$bookingModelRow = $spreadM->getRow ( $whereSpreadModel );
			$data ['doctor_id'] = $bookingModelRow ['duser_id'];
			
			$data ['create_time'] = time ();
			$data ['order_no'] = date ( 'YmdHis' ) . rand ( 10000000, 99999999 );
			$returnid = $bookingModel->add ( $data );
			
			$orderData ['user_id'] = $shop_user ['user_id'];
			$orderData ['order_no'] = $data ['order_no'];
			$orderData ['order_status'] = 1;
			$orderData ['pay_amount'] = 200;
			$orderData ['order_amount'] = 200;
			$orderData ['create_time'] = time ();
			$orderData ['update_time'] = time ();
			$orderModel = new OrderModel ();
			$returnorderid = $orderModel->add ( $orderData );
			$returndata = array (
					'booking_id' => $returnid,
					'order_id' => $returnorderid 
			);
			// 生产订单的同事修改推广记录
			$spreadModel = new SpreadModel ();
			$status = 1;
			$spreadModel->setSpreadStatus ( $user ['openid'], $status );
		}
		else
		{
			$returnid = $bookingModelRow ['id'];
			$bookingModel->modify ( $data, $bookingModelRow ['id'] );
			$bookingData = $bookingModel->getRowById ( $bookingModelRow ['id'] );
			// 获取order表是否有这个订单信息
			$orderModel = new OrderModel ();
			$orderData = $orderModel->getRowOne ( [ 
					'order_no' => $bookingData ['order_no'] 
			] );
			if (! empty ( $orderData ))
			{
				$returnorderid = $orderData ['order_id'];
				$returndata = array (
						'booking_id' => $returnid,
						'order_id' => $returnorderid 
				);
			}
			else
			{
				$returndata = $returnid;
			}
		}
		if ($returndata)
		{
			echo json_encode ( $returndata );
		}
	}
	
	/**
	 * ajax方式发送验证码
	 */
	public function actionSend()
	{
		$msg = [ ];
		
		if ($this->getRequest ()->isAjax == true)
		{
			$user_name = $this->post ( 'user_name' );
			$mobile = $this->post ( 'mobile' );
			$model = new \bc\model\wx\ReportModel ();
			$parmas ['name'] = $user_name;
			$params ['mobile'] = $mobile;
			
			// 13651886355
			
			// 创建一个新的验证码
			
			$arr = [ ];
			
			for($i = 0; $i < 6; $i ++)
			{
				$rand = rand ( 0, 9 );
				$arr [] = $rand;
			}
			
			$code = implode ( '', $arr );
			$data ['code'] = $code;
			$data ['mobile'] = $mobile;
			$data ['create_time'] = time ();
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile 
			] );
			$codeModel->add ( $data );
			
			// 发送短信验证码
			$sms_msg = '验证码' . $code . '，本次验证码60s内有效';
			
			$sms = new Sms ();
			$sms->setMobile ( $mobile );
			$sms->setText ( $sms_msg );
			$result = $sms->send ();
			$msg ['result'] = $result;
			$msg ['msg'] = '验证码已发送!';
		}
		else
		{
			$msg ['msg'] = '请求参数错误！';
		}
		
		echo json_encode ( $msg );
		exit ();
	}
	
	/**
	 * 校验验证码
	 */
	public function actionValidcode()
	{
		$msg ['error'] = 1;
		
		if ($this->getRequest ()->isAjax == true)
		{
			$mobile = $this->post ( 'mobile' );
			$code = $this->post ( 'code' );
			$params ['mobile'] = $mobile;
			
			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [ 
					'mobile' => $mobile,
					'code' => $code 
			] );
			$msg ['data'] = json_encode ( $code_row );
			if (! $code_row [0])
			{
				$msg ['msg'] = '请输入正确的验证码';
			}
			elseif ($code_row [0] ['create_time'] < time () - 60)
			{
				$msg ['msg'] = '验证码已过期';
			}
			else
			{
				$msg ['error'] = 0;
			}
		}
		
		else
		{
			$msg ['msg'] = '请求异常';
		}
		
		echo json_encode ( $msg );
		exit ();
	}

	//订单主页
	public function actionOrder()
	{
		$orderM = new OrderModel();
		return $this->render ( 'orderv', $orderM->getEnvUrl() );
	}

	//查看检测类产品订单
	public function actionOrderlist()
	{
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		if (! $shop_user)
			$this->redirect ( '/' );

		$user_id = $shop_user['user_id'];



		//获取这个人的所有订单
		$orderM = new OrderModel();
		$orderM->columns='bc_product.product_name,bc_product.category_id,
							bc_order.order_id as border_id,bc_order.order_no,bc_order.order_amount,bc_order.pay_amount,bc_order.order_status,
							bc_order_detail.product_id,bc_order_detail.product_num,bc_order_detail.product_price,
							bc_product_sample.product_img,
							bc_order_bind.user_id,bc_order_bind.bind_no,
							bc_order_status.order_status_name
						';
		$orderD = $orderM->getOrderListByFrontend(['bc_order.user_id'=>$user_id]);
		$view['orderlist']=$orderM->dataHandling($orderD);
                
		return $this->render ( 'list', $view );
	}

	//取消订单
	public function actionAjaxcancel()
	{
		$order_id = $this->post ( 'order_id' );

		if(empty($order_id)){
            $data = array(
                'error' => '1',
                'msg' => '订单取消失败'
            );
            echo json_encode($data);
            die();
		}

		$orderModel = new OrderModel();
		$orderModel->modify(['order_status'=>5,'cancellation_time'=>time()],$order_id);
        $data = array(
            'error' => '0',
            'msg' => '订单已取消'
        );
        echo json_encode($data);
        die();
	}

	//根据订单id获取订单详情信息
	public function actionOrderinfo()
	{
		$order_id = $this->get ( 'order_id' );
		$orderModel = new OrderModel();
		$orderModel->columns='bc_product.product_name,bc_product.category_id,
							bc_order.order_id as border_id,bc_order.order_no,bc_order.order_amount,bc_order.pay_amount,bc_order.order_status,bc_order.create_time,bc_order.cancellation_time,bc_order.pay_time,bc_order.send_time,bc_order.completion_time,
							bc_order_detail.product_id,bc_order_detail.product_num,bc_order_detail.product_price,
							bc_product_sample.product_img,
							bc_order_status.order_status_name,bc_order_status.order_status_des,
							bc_order_bind.user_id,
							bc_order_address.addressee,bc_order_address.province,bc_order_address.city,bc_order_address.district,bc_order_address.address,bc_order_address.telephone
						';
		$orderD = $orderModel->getRow(['bc_order.order_id'=>$order_id]);
		$orderData=$orderModel->dataHandling($orderD);
		$view=$orderData[$order_id];
		return $this->render ( 'detail', $view );
	}
	
	
	/**
	 * 优惠券列表
	 */
	public function actionQuickCoupon()
	{
		
		$session = new Session();
		$product_id = $this->get('product_id');
		$productModel = new ProductModel();
		$user = $session->get('shop_user');
		
		$cart = json_decode($session->get('quick_cart'), true);
		
		// 商品信息
		$productModel = new ProductModel ();
		$product = $productModel->getRowById ( $cart['product_id'] );
		$product_price = $product['product_price'];
		$num = $cart['num'];
		
		$product_price = $product_price * $num;
		
		
		//通过分享二维码首次购买商品
		$distributionModel = new DistributionModel();
		$distribution = $distributionModel->getRow(['product_id' => $product['product_id']]);
		if($distribution)
		{
				
			$spokesmanService = new SpokesmanService();
				
			$recommendProductArr = $session->get('recommend_product');
			$recommendProduct = $recommendProductArr[$product_id];
			if($recommendProduct && $spokesmanService->firstBuy($product_id, $user))
			{
					
				$productPrice = $distribution['distribution_price'] - Math::ConverToCeil($distribution['distribution_price'] * $distribution['first_discount'], 1);
				$product_price =  $product_price * $num;
			}
			else
			{
				$product_price =  $distribution['distribution_price'] * $num;
			}
		}
		
		$couponService = new CouponService();
		$params['user_id'] = $user['user_id'];
		$params['product_id'] = $product_id;
		$params['use_start_time'] = $params['use_end_time'] = date('Y-m-d H:i:s');
		$params['product_price'] = $product_price;
		
		$list = $couponService->getAvailableList($params);
//		var_dump($list);
		$view['list'] = $list;
		
		return $this->render('coupon', $view);
		
	}
	
	
	/**
	 * 选择优惠券
	 */
	public function actionChoosecoupon()
	{
		$coupon_id = $this->get('coupon_id');
		$couponModel = new CouponsModel();
		$couponModel->sort = CouponsUserModel::getTableSchema()->name . '.from_user_id ASC';
		$couponList = $couponModel->getAvailableList([CouponsUserModel::getTableSchema()->name.'.coupons_id' => $coupon_id, CouponsModel::getTableSchema()->name . '.is_del' => 0]);
		$coupon = end($couponList);
		
		$session = new Session();
		$session->set('coupon', $coupon);
		
		echo '<script>location.href="/sale/order/quick"</script>';
		
		
	}
	
}

